function face_recognition_UI()
    %% ================ 界面初始化区 ================
    % 创建主窗口
    mainFig = figure('Name', '智能人脸识别系统', ...
                    'NumberTitle', 'off', ...
                    'Position', [100, 100, 1024, 700], ...  % 调整窗口大小为更标准的尺寸
                    'Color', [0.12, 0.12, 0.18], ...
                    'MenuBar', 'none', ...
                    'ToolBar', 'none');
    
    %% ================ 背景设置区 ================
    % 加载科技感背景
    try
        bgImg = imread('科技背景.jpg'); % 请确保此文件存在或替换为您的背景图片路径
    catch
        % 如果找不到背景图片，创建一个高科技感渐变背景
        [X, Y] = meshgrid(1:1200, 1:700);
        bgImg = uint8(zeros(700, 1200, 3));
        bgImg(:,:,1) = uint8(20 + 15*sin(X/150));                  % R通道
        bgImg(:,:,2) = uint8(40 + 25*sin(X/120) + 15*cos(Y/100));  % G通道
        bgImg(:,:,3) = uint8(100 + 40*sin(X/180) + 30*cos(Y/90));  % B通道
        
        % 添加网格线效果
        gridLines = mod(X, 50) < 1 | mod(Y, 50) < 1;
        gridMask = repmat(gridLines, [1, 1, 3]);
        bgImg(gridMask) = min(bgImg(gridMask) + 30, 255);
    end
    
    % 创建背景图像对象
    bgAxes = axes('Parent', mainFig, 'Position', [0, 0, 1, 1]);
    image(bgImg);
    axis off;
    hold on;
    
    % 创建标题
    titleText = uicontrol('Style', 'text', ...
                         'String', '智能人脸识别系统', ...
                         'Position', [400, 650, 400, 40], ...
                         'BackgroundColor', 'none', ...
                         'ForegroundColor', [0.9, 0.9, 1], ...
                         'FontSize', 22, ...
                         'FontWeight', 'bold', ...
                         'FontName', '微软雅黑');
    
    % 添加副标题
    subtitleText = uicontrol('Style', 'text', ...
                            'String', '基于PCA算法的人脸特征提取与识别', ...
                            'Position', [400, 620, 400, 25], ...
                            'BackgroundColor', 'none', ...
                            'ForegroundColor', [0.8, 0.8, 1], ...
                            'FontSize', 12, ...
                            'FontName', '微软雅黑');
    
    %% --- 1. 图像显示区 ---
    % 创建图像对比面板
    imgComparePanel = uipanel('Title', '图像处理对比', ...
                             'TitlePosition', 'centertop', ...
                             'FontSize', 14, ...
                             'FontWeight', 'bold', ...
                             'FontName', '微软雅黑', ...
                             'BackgroundColor', [0.15, 0.15, 0.25, 0.8], ...
                             'ForegroundColor', [0.9, 0.9, 1], ...
                             'Position', [0.4, 0.15, 0.55, 0.6], ...  % 调整位置从0.2到0.15，与图像预处理面板对齐
                             'BorderType', 'line', ...
                             'HighlightColor', [0.5, 0.6, 0.9], ...
                             'BorderWidth', 2);
    
    % 原始图像显示区域
    originalImgPanel = uipanel('Parent', imgComparePanel, ...
                              'Title', '原始图像', ...
                              'TitlePosition', 'centertop', ...
                              'FontSize', 12, ...
                              'FontWeight', 'bold', ...
                              'FontName', '微软雅黑', ...
                              'BackgroundColor', [0.15, 0.15, 0.25], ...
                              'ForegroundColor', [0.9, 0.9, 1], ...
                              'Position', [0.05, 0.1, 0.42, 0.85], ...
                              'BorderType', 'line', ...
                              'HighlightColor', [0.5, 0.6, 0.9]);
    
    originalImgAxes = axes('Parent', originalImgPanel, ...
                          'Position', [0.05, 0.05, 0.9, 0.9], ...
                          'Color', [0.12, 0.12, 0.2]);
    axis(originalImgAxes, 'off');
    
    % 处理后图像显示区域
    processedImgPanel = uipanel('Parent', imgComparePanel, ...
                               'Title', '处理后图像', ...
                               'TitlePosition', 'centertop', ...
                               'FontSize', 12, ...
                               'FontWeight', 'bold', ...
                               'FontName', '微软雅黑', ...
                               'BackgroundColor', [0.15, 0.15, 0.25], ...
                               'ForegroundColor', [0.9, 0.9, 1], ...
                               'Position', [0.53, 0.1, 0.42, 0.85], ...
                               'BorderType', 'line', ...
                               'HighlightColor', [0.5, 0.6, 0.9]);
    
    processedImgAxes = axes('Parent', processedImgPanel, ...
                           'Position', [0.05, 0.05, 0.9, 0.9], ...
                           'Color', [0.12, 0.12, 0.2]);
    axis(processedImgAxes, 'off');
    
    % 添加默认显示
    try
        defaultImg = imread('face_icon.png'); % 请准备一个人脸轮廓图标
        imshow(defaultImg, 'Parent', originalImgAxes);
        imshow(defaultImg, 'Parent', processedImgAxes);
    catch
        text(originalImgAxes, 0.5, 0.5, '请选择待识别图像', ...
             'HorizontalAlignment', 'center', ...
             'FontSize', 14, ...
             'FontName', '微软雅黑', ...
             'Color', [0.7, 0.7, 0.9]);
        text(processedImgAxes, 0.5, 0.5, '处理后图像将显示在此', ...
             'HorizontalAlignment', 'center', ...
             'FontSize', 14, ...
             'FontName', '微软雅黑', ...
             'Color', [0.7, 0.7, 0.9]);
    end
    
    % 添加图像边框效果
    rectangle('Parent', originalImgAxes, 'Position', [0, 0, 1, 1], ...
              'EdgeColor', [0.5, 0.6, 0.9], 'LineWidth', 2, ...
              'LineStyle', '-');
    rectangle('Parent', processedImgAxes, 'Position', [0, 0, 1, 1], ...
              'EdgeColor', [0.5, 0.6, 0.9], 'LineWidth', 2, ...
              'LineStyle', '-');
    
    %% --- 2. 控制区 ---
    ctrlPanel = uipanel('Title', '操作控制区', ...
                    'TitlePosition', 'centertop', ...
                    'FontSize', 14, ...
                    'FontWeight', 'bold', ...
                    'FontName', '微软雅黑', ...
                    'BackgroundColor', [0.15, 0.15, 0.25, 0.8], ...
                    'ForegroundColor', [0.9, 0.9, 1], ...
                    'Position', [0.05, 0.6, 0.3, 0.2], ...
                    'BorderType', 'line', ...
                    'BorderWidth', 2, ...
                    'HighlightColor', [0.5, 0.6, 0.9]);

    % 选择输入待识别图像按钮
    inputBtn = uicontrol('Parent', ctrlPanel, ...
                        'Style', 'pushbutton', ...
                        'String', '选择图像', ...
                        'Position', [30, 80, 100, 35], ...
                        'BackgroundColor', [0.3, 0.4, 0.7], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 12, ...
                        'FontName', '微软雅黑', ...
                        'FontWeight', 'bold', ...
                        'Callback', @(src, event) face_recognition_functions('selectImage', mainFig, originalImgAxes, processedImgAxes));

    % 清除待识别图像按钮
    clearBtn = uicontrol('Parent', ctrlPanel, ...
                        'Style', 'pushbutton', ...
                        'String', '清除图像', ...
                        'Position', [170, 80, 100, 35], ...
                        'BackgroundColor', [0.3, 0.4, 0.7], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 12, ...
                        'FontName', '微软雅黑', ...
                        'Callback', @(src, event) face_recognition_functions('clearImage', mainFig, originalImgAxes, processedImgAxes));

    % 摄像头控制按钮
    cameraStartBtn = uicontrol('Parent', ctrlPanel, ...
                            'Style', 'pushbutton', ...
                            'String', '启动摄像头', ...
                            'Position', [30, 30, 100, 35], ...
                            'BackgroundColor', [0.3, 0.7, 0.4], ...
                            'ForegroundColor', [1, 1, 1], ...
                            'FontSize', 12, ...
                            'FontName', '微软雅黑', ...
                            'Callback', @(src, event) face_recognition_functions('startCamera', mainFig, processedImgAxes));

    cameraStopBtn = uicontrol('Parent', ctrlPanel, ...
                            'Style', 'pushbutton', ...
                            'String', '停止摄像头', ...
                            'Position', [170, 30, 100, 35], ...
                            'BackgroundColor', [0.7, 0.3, 0.3], ...
                            'ForegroundColor', [1, 1, 1], ...
                            'FontSize', 12, ...
                            'FontName', '微软雅黑', ...
                            'Callback', @(src, event) face_recognition_functions('stopCamera', mainFig));

    cameraCaptureBtn = uicontrol('Parent', ctrlPanel, ...
                            'Style', 'pushbutton', ...
                            'String', '捕获图像', ...
                            'Position', [100, 5, 100, 20], ...
                            'BackgroundColor', [0.3, 0.4, 0.7], ...
                            'ForegroundColor', [1, 1, 1], ...
                            'FontSize', 10, ...
                            'FontName', '微软雅黑', ...
                            'Callback', @(src, event) face_recognition_functions('captureFromCamera', mainFig, originalImgAxes, processedImgAxes));

    % 开始识别按钮 - 居中放置
    recognizeBtn = uicontrol('Parent', ctrlPanel, ...
                            'Style', 'pushbutton', ...
                            'String', '开始识别', ...
                            'Position', [100, 30, 100, 40], ...
                            'BackgroundColor', [0.2, 0.6, 0.8], ...
                            'ForegroundColor', [1, 1, 1], ...
                            'FontSize', 14, ...
                            'FontName', '微软雅黑', ...
                            'FontWeight', 'bold', ...
                            'Callback', @(src, event) face_recognition_functions('startRecognition', mainFig, resultText, matchText, matchAxes));
    
    % --- 图像处理控制面板 ---
    imgProcessPanel = uipanel('Title', '图像预处理', ...
                             'TitlePosition', 'centertop', ...
                             'FontSize', 14, ...
                             'FontWeight', 'bold', ...
                             'FontName', '微软雅黑', ...
                             'BackgroundColor', [0.15, 0.15, 0.25, 0.8], ...
                             'ForegroundColor', [0.9, 0.9, 1], ...
                             'Position', [0.05, 0.1, 0.3, 0.45], ...  % 调整高度从0.35增加到0.4，并向下移动位置从0.2到0.15
                             'BorderType', 'line', ...
                             'BorderWidth', 2, ...
                             'HighlightColor', [0.5, 0.6, 0.9]);
    
    % 按钮大小和间距设置
    btnWidth = 100;
    btnHeight = 30;
    hMargin = 20;  % 水平边距
    vSpacing = 12; % 垂直间距从15减小到12
    topPos = 250;  % 顶部起始位置
    
    % 第一行按钮 - 重置图像和灰度化
    resetBtn = uicontrol('Parent', imgProcessPanel, ...
                        'Style', 'pushbutton', ...
                        'String', '重置图像', ...
                        'Position', [hMargin, topPos, btnWidth, btnHeight], ...
                        'BackgroundColor', [0.3, 0.4, 0.7], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 12, ...
                        'FontName', '微软雅黑', ...
                        'Callback', @(src, event) face_recognition_functions('resetImage', mainFig, processedImgAxes));
    
    grayBtn = uicontrol('Parent', imgProcessPanel, ...
                        'Style', 'pushbutton', ...
                        'String', '灰度化', ...
                        'Position', [2*hMargin+btnWidth, topPos, btnWidth, btnHeight], ...
                        'BackgroundColor', [0.3, 0.4, 0.7], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 12, ...
                        'FontName', '微软雅黑', ...
                        'Callback', @(src, event) face_recognition_functions('convertToGrayscale', mainFig, processedImgAxes));
    
    % 第二行按钮 - 直方均衡化
    histEqBtn = uicontrol('Parent', imgProcessPanel, ...
                        'Style', 'pushbutton', ...
                        'String', '直方均衡化', ...
                        'Position', [hMargin + btnWidth/2, topPos-btnHeight-vSpacing, btnWidth, btnHeight], ...
                        'BackgroundColor', [0.3, 0.4, 0.7], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 12, ...
                        'FontName', '微软雅黑', ...
                        'Callback', @(src, event) face_recognition_functions('histogramEqualization', mainFig, processedImgAxes));
    
    % 旋转控制 - 标签和滑块
    uicontrol('Parent', imgProcessPanel, ...
             'Style', 'text', ...
             'String', '旋转角度:', ...
             'Position', [hMargin, topPos-2*(btnHeight+vSpacing), 80, 20], ...
             'BackgroundColor', 'none', ...
             'ForegroundColor', [0.9, 0.9, 1], ...
             'FontSize', 11, ...
             'FontName', '微软雅黑', ...
             'HorizontalAlignment', 'left');
    
    rotationSlider = uicontrol('Parent', imgProcessPanel, ...
                              'Style', 'slider', ...
                              'Min', -180, ...
                              'Max', 180, ...
                              'Value', 0, ...
                              'SliderStep', [1/360, 10/360], ...
                              'Position', [hMargin, topPos-2*(btnHeight+vSpacing)-25, 200, 20], ...
                              'BackgroundColor', [0.25, 0.3, 0.5], ...
                              'Callback', @(src, event) face_recognition_functions('rotateImage', mainFig, processedImgAxes, src));
    
    rotationText = uicontrol('Parent', imgProcessPanel, ...
                            'Style', 'text', ...
                            'String', '0°', ...
                            'Position', [hMargin+205, topPos-2*(btnHeight+vSpacing)-25, 40, 20], ...
                            'BackgroundColor', 'none', ...
                            'ForegroundColor', [0.9, 0.9, 1], ...
                            'FontSize', 11, ...
                            'FontName', '微软雅黑');
    
    % 裁切控制 - 标签和滑块
    uicontrol('Parent', imgProcessPanel, ...
             'Style', 'text', ...
             'String', '裁剪比例:', ...
             'Position', [hMargin, topPos-3*(btnHeight+vSpacing)-10, 80, 20], ...
             'BackgroundColor', 'none', ...
             'ForegroundColor', [0.9, 0.9, 1], ...
             'FontSize', 11, ...
             'FontName', '微软雅黑', ...
             'HorizontalAlignment', 'left');
    
    cropSlider = uicontrol('Parent', imgProcessPanel, ...
                          'Style', 'slider', ...
                          'Min', 0.1, ...
                          'Max', 1, ...
                          'Value', 1, ...
                          'SliderStep', [0.05, 0.2], ...
                          'Position', [hMargin, topPos-3*(btnHeight+vSpacing)-35, 200, 20], ...
                          'BackgroundColor', [0.25, 0.3, 0.5], ...
                          'Callback', @(src, event) face_recognition_functions('cropImage', mainFig, processedImgAxes, src));
    
    cropText = uicontrol('Parent', imgProcessPanel, ...
                        'Style', 'text', ...
                        'String', '100%', ...
                        'Position', [hMargin+205, topPos-3*(btnHeight+vSpacing)-35, 40, 20], ...
                        'BackgroundColor', 'none', ...
                        'ForegroundColor', [0.9, 0.9, 1], ...
                        'FontSize', 11, ...
                        'FontName', '微软雅黑');
    
    % 翻转控制 - 标签
    uicontrol('Parent', imgProcessPanel, ...
             'Style', 'text', ...
             'String', '图像翻转:', ...
             'Position', [hMargin, topPos-4*(btnHeight+vSpacing)-20, 80, 20], ...
             'BackgroundColor', 'none', ...
             'ForegroundColor', [0.9, 0.9, 1], ...
             'FontSize', 11, ...
             'FontName', '微软雅黑', ...
             'HorizontalAlignment', 'left');
    
    % 翻转按钮对称排列
    flipHBtn = uicontrol('Parent', imgProcessPanel, ...
                        'Style', 'pushbutton', ...
                        'String', '水平翻转', ...
                        'Position', [hMargin, topPos-4*(btnHeight+vSpacing)-45, btnWidth, btnHeight], ...
                        'BackgroundColor', [0.3, 0.4, 0.7], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 11, ...
                        'FontName', '微软雅黑', ...
                        'Callback', @(src, event) face_recognition_functions('flipImageH', mainFig, processedImgAxes));
    
    flipVBtn = uicontrol('Parent', imgProcessPanel, ...
                        'Style', 'pushbutton', ...
                        'String', '垂直翻转', ...
                        'Position', [2*hMargin+btnWidth, topPos-4*(btnHeight+vSpacing)-45, btnWidth, btnHeight], ...
                        'BackgroundColor', [0.3, 0.4, 0.7], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 11, ...
                        'FontName', '微软雅黑', ...
                        'Callback', @(src, event) face_recognition_functions('flipImageV', mainFig, processedImgAxes));
    
    %% ================ 数据初始化区 ================
    % 全局变量
    userData = struct('originalImg', [], 'processedImg', [], 'trueOriginalImg', [], 'matchResult', []);
    setappdata(mainFig, 'UserData', userData);
    
    % 创建一个隐藏的结果控件结构体，以便回调函数访问
    % 注意：虽然删除了结果显示区，但为了保持函数兼容性，我们仍然创建这些控件但不显示它们
    resultText = uicontrol('Parent', mainFig, 'Visible', 'off', 'Style', 'text');
    matchText = uicontrol('Parent', mainFig, 'Visible', 'off', 'Style', 'text');
    matchAxes = axes('Parent', mainFig, 'Visible', 'off');
    
    % 将结果显示控件保存到应用数据中，以便回调函数访问
    setappdata(mainFig, 'ResultControls', struct('resultText', resultText, 'matchText', matchText, 'matchAxes', matchAxes));
    
    % 将图像显示区域保存到应用数据中，以便回调函数访问
    setappdata(mainFig, 'ImageAxes', struct('originalImgAxes', originalImgAxes, 'processedImgAxes', processedImgAxes));
end

%% ================ 辅助函数 ================
% 标签页切换函数
function switchToTab(tabGroup, tabIndex)
    tabGroup.SelectedIndex = tabIndex;
end

% 开始识别并显示结果
function startRecognitionAndShowResult(mainFig, tabGroup)
    % 获取UI元素
    userData = getappdata(mainFig, 'UserData');
    resultControls = getappdata(mainFig, 'ResultControls');
    imageAxes = getappdata(mainFig, 'ImageAxes');
    
    % 获取处理后的图像
    processedImg = userData.processedImg;
    
    % 如果没有图像，提示用户
    if isempty(processedImg)
        warndlg('请先选择并处理图像！', '提示');
        return;
    end
    
    % 显示等待提示
    waitText = uicontrol('Parent', mainFig, ...
                        'Style', 'text', ...
                        'String', '正在识别中，请稍候...', ...
                        'Position', [500, 350, 200, 30], ...
                        'BackgroundColor', [0.2, 0.2, 0.3], ...
                        'ForegroundColor', [1, 1, 1], ...
                        'FontSize', 12, ...
                        'FontName', '微软雅黑');
    drawnow;
    
    try
        % 这里添加实际的人脸识别代码
        [matchedImg, similarity, features] = performFaceRecognition(processedImg);
        
        % 删除等待提示
        delete(waitText);
        
        % 显示结果
        imshow(processedImg, 'Parent', userData.resultOriginalAxes);
        
        % 显示匹配图像
        if ~isempty(matchedImg)
            imshow(matchedImg, 'Parent', userData.matchedImgAxes);
        else
            % 如果没有匹配结果，显示默认图像
            text(userData.matchedImgAxes, 0.5, 0.5, '未找到匹配', ...
                 'HorizontalAlignment', 'center', ...
                 'FontSize', 14, ...
                 'FontName', '微软雅黑', ...
                 'Color', [0.9, 0.5, 0.5]);
        end
        
        % 更新识别结果文本
        if similarity > 0.8
            set(userData.resultText, 'String', '识别成功！', 'ForegroundColor', [0.2, 0.9, 0.2]);
        elseif similarity > 0.6
            set(userData.resultText, 'String', '可能匹配', 'ForegroundColor', [0.9, 0.9, 0.2]);
        else
            set(userData.resultText, 'String', '识别失败', 'ForegroundColor', [0.9, 0.2, 0.2]);
        end
        
        % 更新相似度和处理时间
        set(userData.similarityText, 'String', sprintf('相似度: %.2f%%', similarity * 100));
        set(userData.timeText, 'String', sprintf('处理时间: %.2f ms', processingTime * 1000));
        
        % 更新特征列表
        if ~isempty(features)
            featureStrs = cell(length(features), 1);
            for i = 1:length(features)
                featureStrs{i} = sprintf('特征 %d: %.4f', i, features(i));
            end
            set(userData.featureListbox, 'String', featureStrs);
        else
            set(userData.featureListbox, 'String', '无特征数据');
        end
        
    catch e
        % 错误处理
        errordlg(['识别过程中出错: ' e.message], '错误');
    end
    
    % 删除等待提示
    delete(waitText);
end

% 人脸识别算法（示例函数，需要替换为实际的识别算法）
function [matchedImg, similarity, features] = performFaceRecognition(inputImg)
    % 这里应该是实际的人脸识别算法
    % 为了演示，我们创建一个模拟的结果
    
    % 模拟处理延迟
    pause(1);
    
    % 模拟匹配图像（实际应用中应该从数据库中检索）
    try
        % 尝试加载一个示例匹配图像
        matchedImg = imread('matched_face.jpg');
    catch
        % 如果找不到，就使用输入图像并添加一些变化
        matchedImg = inputImg;
        if size(matchedImg, 3) == 3
            % 对RGB图像进行一些变化以模拟不同的人脸
            matchedImg(:,:,1) = uint8(double(matchedImg(:,:,1)) * 0.9);
            matchedImg(:,:,2) = uint8(double(matchedImg(:,:,2)) * 1.1);
        end
    end
    
    % 模拟相似度（0-1之间的随机值，偏向高值）
    similarity = 0.7 + 0.3 * rand();
    
    % 模拟特征向量（实际应用中这应该是PCA或其他算法提取的特征）
    features = rand(10, 1);
end